package com.brewster.config.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法 详见 JFinal 俱乐部:
 * http://jfinal.com/club
 * 
 * Blog model. 数据库字段名建议使用驼峰命名规则，便于与 java 代码保持一致，如字段名： userId
 */
@SuppressWarnings("serial")
public class Menu extends Model<Menu> {
	public static final Menu dao = new Menu().dao();
	public final static int NORMAL = 0;
	public final static int DELETE = 1;

	/**
	 * 鏍规嵁瑙掕壊鏌ヨ鑿滃崟琛?
	 */
	public List<Menu> queryMenuByRole(int accountid) {
		StringBuffer sb = new StringBuffer("SELECT m.menu_id ,m.menu_name,m.menu_desc,m.menu_url,m.images,ma.id ");
		sb.append("FROM da_menu m LEFT JOIN da_role_menu rm ON rm.menu_id = m.menu_id ");
		sb.append("LEFT JOIN da_account_role ar ON ar.role_id = rm.role_id ");
		sb.append("LEFT JOIN da_menu_account ma ON ma.menuid = m.menu_id ");
		sb.append("WHERE ar.account_id = ? AND m.del_flag = ? AND m.`level` = 'level2' GROUP BY m.menu_id ");
		List<Menu> menu = dao.find(sb.toString(), accountid, NORMAL);
		return menu;
	}

	/**
	 * 鏍规嵁鐢ㄦ埛id鏌ヨ鑿滃崟琛?
	 */
	public List<Menu> queryMenuByAccount(int accountid) {
		StringBuffer sb = new StringBuffer("SELECT 	m.menu_id ,m.menu_name,m.menu_desc,m.menu_url,m.images,ma.id ");
		sb.append("FROM da_menu m LEFT JOIN da_menu_account ma ON ma.menuid = m.menu_id WHERE ma.accountid = ?");
		List<Menu> menu = dao.find(sb.toString(), accountid);
		return menu;
	}

	public List<Menu> getButtonsByRoleids(String ids) {

		return Menu.dao.find(
				"select permission from da_role_menu t left join da_menu t1 on t.menu_id  = t1.menu_id where t.role_id in ("
						+ ids + ") and t1.level is null and t1.del_flag = 0");
	}

	public List<Menu> getMenusByRoleId(String roleId) {
		System.out.println(roleId);
		return Menu.dao.find(
				" SELECT t.*  from da_menu t inner JOIN (select * from da_role_menu t2 where t2.role_id in (" + roleId
						+ ") group by menu_id) t1 on t.menu_id = t1.menu_id where  t.del_flag =? and t.level is not null and  t.level !='' and t.menu_id >5 order by level ,sort ",
				0);
//	        return Menu.dao.find(" SELECT t.*, CASE t1.id when null then '' else 'true' end as checked from da_menu t left JOIN (select * from da_role_menu t2 where t2.role_id=?) t1 on t.menu_id = t1.menu_id where  t.del_flag =? order by sort asc",roleId,EnumFuctionStatus.NORMAL_STATUS.getCode());
	}
}
